/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Application;

import Maps.GoogleMaps;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author Stanley
 */
public class FindCoordinates extends Form implements CommandListener {
    private  MainMidlet midlet;
    private TextField latitude;
    private TextField longitude;
    // command
    private Command find;
    private Command cancel;
    
    public FindCoordinates(MainMidlet midlet) {
        super("Find coordinates", null);
        
        latitude = new TextField("Latitude :", null, 12, TextField.DECIMAL);
        longitude = new TextField("Longitude : ", null, 12, TextField.DECIMAL);
        append(latitude);
        append(longitude);
        
        this.midlet = midlet;
        // command
        find = new Command("Find", Command.OK, 1);
        cancel = new Command("Cancel", Command.CANCEL, 1);
        setCommandListener(this);
        addCommand(find);
        addCommand(cancel);
    }
    
    public void commandAction(Command c, Displayable d) {
        if (c == cancel) {
            midlet.homeShow();
        } else if (c == find) {
            double[] lanLng = new double[2];
            lanLng[0] = Double.parseDouble(latitude.getString());
            lanLng[1] = Double.parseDouble(longitude.getString());
            int zoom = midlet.getHome().getZoomLevel();
            try {
                GoogleMaps maps = new GoogleMaps();
                Image map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoom, "png32", MapType.DEFAULT);
                // set maps and coordinates into home screen
                midlet.getHome().setLanLng(lanLng);
                midlet.getHome().setMap(map);
                midlet.homeShow();
            } catch (Exception e) {
            }
        }
    }
}
